//
//  Emoticon.swift
//  表情键盘
//
//  Created by LiHong on 15/11/6.
//  Copyright © 2015年 LiHong. All rights reserved.
//

import UIKit
//MARK: - 表情包模型
///表情包模型
class EmoticonPakage: NSObject {
    //MARK: - 属性
    private static let bundlePath = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
    
    ///文件夹名称
    var id: String?
    
    ///表情包名称
    var group_name_cn: String?
    
    ///所有表情
    var emoticons: [Emoticon]?
    
    ///构造方法
    init(id: String) {
        self.id = id
        super.init()
    }
    
    ///打印方法
    override var description: String {
        return "\n\t表情包模型: id: \(id), group_name_cn:\(group_name_cn), emoticons: \(emoticons)"
    }
    
    static let packages: [EmoticonPakage] = EmoticonPakage.loadPackages()
    
    ///加载表情包
    class func loadPackages() -> [EmoticonPakage] {
        //拼接路径
        let plistPath = bundlePath + "/emoticons.plist"
        //加载plist
        let plistDict = NSDictionary(contentsOfFile: plistPath)!
        ///表情包数组
        var packages = [EmoticonPakage]()
        
        // 创建“最近”
        let recent = EmoticonPakage(id: "")
        
        packages.append(recent)
        
        recent.emoticons = [Emoticon]()
        
        recent.group_name_cn = "最近"
        //添加空表情和删除按钮
        recent.appendEmptyEmoticon()
        
        
        // 获取数组里每个字典的key为id的值
        if let packageArray = plistDict["packages"] as? [[String: AnyObject]] {
            //遍历数组
            for dict in packageArray {
                let id = dict["id"] as! String
                
                let package = EmoticonPakage(id: id)
                
                package.loadEmoticon()
                
                packages.append(package)
            }
            
            
        }
        
        return packages
        
    }
    ///加载表情
    func loadEmoticon() {
        
        let infoPath = EmoticonPakage.bundlePath + "/" + id! + "/info.plist"
       // print(infoPath)
        let infoDict = NSDictionary(contentsOfFile: infoPath)!
        // 获取表情包名称
        group_name_cn = infoDict["group_name_cn"] as? String
        // 创建表情模型数组
        emoticons = [Emoticon]()
        
        var index = 0
        if let array = infoDict["emoticons"] as? [[String: String]] {
            for dict in array {
                
                emoticons?.append(Emoticon(id: id!, dict: dict))
                
                index++
                
                if index == 20 {
                    emoticons?.append(Emoticon(removeEmoticon: true))
                    index = 0
                }
            }
         appendEmptyEmoticon()
        }
    }
    /// 添加空白按钮
    private func appendEmptyEmoticon() {
        let count = emoticons!.count % 21
        
        if count > 0 || emoticons!.count == 0{
            for _ in count..<20 {
                emoticons?.append(Emoticon(removeEmoticon: false))
                
            }
            emoticons?.append(Emoticon(removeEmoticon: true))
        }
        
        
    }
    /// 添加最近表情
    class func addFavorate(emoticon: Emoticon) {
        
        if emoticon.removeEmoticon {
          //  print("去除")
            return
        }
        
        emoticon.times++
        
        var recentEmoticonPackage = packages[0].emoticons!
        
        let removeEmoticon = recentEmoticonPackage.removeLast()
        
        let contains = recentEmoticonPackage.contains(emoticon)
        if !contains {
            //添加表情
            recentEmoticonPackage.append(emoticon)
        }
        
        //排序
        recentEmoticonPackage = recentEmoticonPackage.sort({ (e1, e2) -> Bool in
            return e1.times > e2.times
        })
        
        if !contains {
            recentEmoticonPackage.removeLast()
        }
        
        recentEmoticonPackage.append(removeEmoticon)
        
        packages[0].emoticons = recentEmoticonPackage
    }
    
    
}
class Emoticon: NSObject {

    // 属性
    /// 记录点击次数
    var times = 0
    
    /// 表情包文件名称
    var id: String?
    
    /// 表情名称，用于网络传输
    var chs: String?
    
    /// 表情图片对应的名称
    var png: String? {
        didSet {
            pngPath = EmoticonPakage.bundlePath + "/" + id! + "/" + png!
        }
    }
    
    /// 完整的图片路径
    var pngPath: String?
    
    /// emoji表情对应的16进制字符串
    var code: String? {
        didSet {
            guard let co = code else {
                return
            }
            
            let scanner = NSScanner(string: co)
            
            var value: UInt32 = 0
            
            scanner.scanHexInt(&value)
            
            let c = Character(UnicodeScalar(value))
            emoji = String(c)
            
        }
    }
    
    /// emoji表情
    var emoji: String?
    
    /// true表示带删除按钮的表情模型，false 表示空模型
    var removeEmoticon: Bool = false
    
    init(removeEmoticon: Bool) {
        self.removeEmoticon = removeEmoticon
        super.init()
    }
    

    
    init(id: String, dict: [String: AnyObject]) {
        self.id = id
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    /// 打印方法
    override var description: String {
        return "\n\t\t表情模型: chs: \(chs), png: \(png), code: \(code))"
    }
    
    
    
    
    
}


